package com.lincolnXmypcqq.config;

import com.lincoln.framework.NettyServer;
import com.lincolnXmypcqq.cmds.framework.RobotCmdFactory;
import com.lincolnXmypcqq.netty.LoginExecute;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 启动服务器前执行
 * Date:2017/8/21.
 * Time:15:36
 */
public class StartRun implements CommandLineRunner, ApplicationListener<ContextRefreshedEvent> {
    private static ApplicationContext applicationContext = null;

    @Override
    public void run(String... args) throws Exception {
        RobotCmdFactory.init();
        NettyServer.scanProtocol(1, 1, "com.lincolnXmypcqq.netty");
        NettyServer.addDefaultExecute(new LoginExecute());
        NettyServer.runServer();
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (applicationContext == null) {
            applicationContext = contextRefreshedEvent.getApplicationContext();
        }
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
}
